package com.bitcat.firstapp.views

import android.app.Dialog
import android.content.Context
import android.os.Bundle
import com.bitcat.firstapp.databinding.DialogCustom2Binding

class CustomDialog(context: Context) : Dialog(context) {

    private lateinit var binding: DialogCustom2Binding

    private var onConfirmed: ((String) -> Unit)? = null


    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        binding = DialogCustom2Binding.inflate(layoutInflater)
        setContentView(binding.root)

        //设置对话框的宽度
        val params = window?.attributes
        //获取手机屏幕的宽度
        val screenWidth = (context.resources.displayMetrics.widthPixels * 0.8).toInt()
        params?.width = screenWidth


        //设置点击对话框外部不消失
        setCanceledOnTouchOutside(false)


        binding.btnCancel.setOnClickListener {
            dismiss()
        }

        binding.btnConfirm.setOnClickListener {
            val content = binding.etContent.text.toString()
            this.onConfirmed?.invoke(content)
        }

    }


    fun setConfirmedListener(listener: (String) -> Unit) {
        this.onConfirmed = listener
    }
}